package model;

import dao.UserDao;
import dao.DaoFactory;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 
 * This class is User model class
 * 
 * @author Sukhjinder K. Narula (narula.sk@gmail.com)
 * @since May 2013
 */
public class User {
	private final UserDao userDao;
	private int id;
	private String name;
//	private String error;
	private List<Folder> folderList;

	/**
	 * Constructor
	 * 
	 * @param name
	 */
	public User(String name) {
		this.name = name;
		this.userDao = DaoFactory.getUserDao();
		this.folderList = new ArrayList<Folder>();
	}

	/**
	 * 
	 * @return id
	 */
	public int getId() {
		return id;
	}


	/**
	 * 
	 * @return name
	 */
	public String getName() {
		return name;
	}

	/**
	 * 
	 * @param name
	 */
	public void setName(String name) {
		this.name = name;
	}

//	/**
//	 * 
//	 * @return error
//	 */
//	public String getError() {
//		return error;
//	}

//	/**
//	 * 
//	 * @param name
//	 */
//	public void setError(String error) {
//		this.error = error;
//	}

	/**
	 * 
	 * @return boolean
	 */
	public boolean insert() {
		if (checkIfUserExists(this.name)) {
			return false;
		}
		try {
			this.id = userDao.insert(createProperties());
			/*
			 * THERE CAN BE DB ERRORS HERE, NEED TO HANDLE THEM
			 */
			Folder inbox = new Folder("Inbox", this.id);
			inbox.insert();
			Folder outbox = new Folder("Outbox", this.id);
			outbox.insert();
			Folder sent = new Folder("Sent", this.id);
			sent.insert();
			Folder drafts = new Folder("Drafts", this.id);
			drafts.insert();
			this.loadFolders();
		} catch (Exception e) {
//			this.setError("Problem connecting to DB");
			return false;
		}
		return true;
	}

	/**
	 * Loads up a user from the database including all the folders and emails
	 * @return boolean
	 */
	public boolean load() {
		
		Map<String, String> result = null;
		try {
			result = userDao.load(name);
		} catch (Exception e) {
			return false;
		}
		if (result.containsKey("error")) {
			return false;
		}
		this.id = Integer.parseInt(result.get(id));
		this.loadFolders();
		return true;
	}

	/**
	 * 
	 * @return boolean
	 */
	public boolean update() {
		try {
			this.userDao.update(createProperties());
		} catch (Exception e) {
//			this.setError("Problem connecting to DB");
			return false;
		}
		return true;
	}

	/**
	 * Delete the email from the db
	 */
	public boolean delete() {
		try {
			this.userDao.delete(this.getId());
		} catch (Exception e) {
//			this.setError("Problem connecting to DB");
			return false;
		}
		return true;
	}

	/**
	 * @return String
	 */
	public String toString() {
		return "";
	}

	/**
	 * 
	 * @return properties
	 */
	@SuppressWarnings("serial")
	private Map<String, String> createProperties() {
		return new HashMap<String, String>() {
			{
				put("id", String.valueOf(id));
				put("name", getName());
			}
		};
	}

	/**
	 * 
	 * @return boolean
	 */
	public boolean checkIfUserExists(String name) {
		Map<String, String> userProperties = null;
		try {
			userProperties = userDao.load(name);
		} catch (Exception e) {
//			this.setError("Problem connecting to db");
			return false;
		}
		if (!userProperties.containsKey("error")) {
			return false;
		}
//		this.setError(userProperties.get("error"));
		return true;
	}

	/**
	 * Loads the folders
	 * @return boolean
	 */
	public boolean loadFolders() {
		Map<Integer, String[]> userFolder = null;
		try {
			userFolder = DaoFactory.getFolderDao().selectUserFolders(this.getId());
			if (userFolder == null) {
//				this.setError("NotFound");
			}
			for (Map.Entry<Integer, String[]> entry : userFolder.entrySet()) {
				String folder[] = entry.getValue();
				Folder folderObj = new Folder(folder[0], Integer.parseInt(folder[1]));
				folderObj.setId(entry.getKey());
				folderObj.loadEmails();
				folderList.add(folderObj);
			}
		} catch (Exception e) {
//			this.setError("Problem connecting to db");
			return false;
		}

		return true;
	}
}
